package com.cw.jeeyt.service.impl.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemDatabaseColumnBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemDatabaseColumnDao;
import com.cw.jeeyt.dao.model.system.SystemDatabaseColumnDo;
import com.cw.jeeyt.service.converter.system.SystemDatabaseColumnConverter;
import com.cw.jeeyt.service.intf.system.SystemDatabaseColumnService;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 数据库字段信息业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SystemDatabaseColumnServiceImpl implements SystemDatabaseColumnService {

    private static final Logger logger = LoggerFactory.getLogger(SystemDatabaseColumnServiceImpl.class);
    @Autowired
    private SystemDatabaseColumnDao systemDatabaseColumnDao;

    /**
     * 新增SystemDatabaseColumn
     *
     * @param systemDatabaseColumnBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSystemDatabaseColumn(SystemDatabaseColumnBo systemDatabaseColumnBo) throws Exception {

        if (systemDatabaseColumnBo == null) {
            logger.error("数据库字段信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        SystemDatabaseColumnDo systemDatabaseColumn = SystemDatabaseColumnConverter.bo2Do(systemDatabaseColumnBo);
        systemDatabaseColumn.setId(SequenceTool.nextId());
        systemDatabaseColumn.setCreateBy(userVo.getId());
        systemDatabaseColumn.setCreateDate(new Date());
        systemDatabaseColumn.setUpdateBy(userVo.getId());
        systemDatabaseColumn.setUpdateDate(new Date());
        systemDatabaseColumnDao.insertSystemDatabaseColumn(systemDatabaseColumn);
    }

    /**
     * 新增SystemDatabaseColumn
     *
     * @param systemDatabaseColumnList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSystemDatabaseColumnBat(List<SystemDatabaseColumnBo> systemDatabaseColumnList) throws Exception {

        if (systemDatabaseColumnList == null || systemDatabaseColumnList.isEmpty()) {
            logger.error("要保存数据库字段信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        List<SystemDatabaseColumnDo> sdcList = new ArrayList<SystemDatabaseColumnDo>();
        for (SystemDatabaseColumnBo sdc : systemDatabaseColumnList) {
            SystemDatabaseColumnDo systemDatabaseColumn = SystemDatabaseColumnConverter.bo2Do(sdc);
            systemDatabaseColumn.setId(SequenceTool.nextId());
            systemDatabaseColumn.setCreateBy(userVo.getId());
            systemDatabaseColumn.setCreateDate(new Date());
            systemDatabaseColumn.setUpdateBy(userVo.getId());
            systemDatabaseColumn.setUpdateDate(new Date());
            sdcList.add(systemDatabaseColumn);
        }
        systemDatabaseColumnDao.insertSystemDatabaseColumnBat(sdcList);
    }

    @Override
    public List<SystemDatabaseColumnBo> queryList() throws Exception {
        return SystemDatabaseColumnConverter.doList2BoList(systemDatabaseColumnDao.querySystemDatabaseColumnList());
    }

    /**
     * 更新SystemDatabaseColumn信息
     *
     * @param systemDatabaseColumnBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSystemDatabaseColumn(SystemDatabaseColumnBo systemDatabaseColumnBo) throws Exception {

        if (systemDatabaseColumnBo == null) {
            logger.error("数据库字段信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        SystemDatabaseColumnDo systemDatabaseColumn = SystemDatabaseColumnConverter.bo2Do(systemDatabaseColumnBo);
        systemDatabaseColumn.setUpdateBy(userVo.getId());
        systemDatabaseColumn.setUpdateDate(new Date());
        systemDatabaseColumnDao.updateSystemDatabaseColumn(systemDatabaseColumn);
    }

    /**
     * 删除SystemDatabaseColumn
     *
     * @param systemDatabaseColumnBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSystemDatabaseColumn(SystemDatabaseColumnBo systemDatabaseColumnBo) throws Exception {
        if (systemDatabaseColumnBo == null) {
            logger.error("数据库字段信息为空");
            return;
        }
        systemDatabaseColumnDao.deleteSystemDatabaseColumn(SystemDatabaseColumnConverter.bo2Do(systemDatabaseColumnBo));

    }

    /**
     * 根据表id删除表字段信息
     */
    @Override
    public void deleteByTableId(String tableId) throws Exception {
        if (StringUtils.isEmpty(tableId)) {
            logger.error("数据库字段信息为空");
            return;
        }
        systemDatabaseColumnDao.deleteByTableId(tableId);
    }

    @Override
    public List<SystemDatabaseColumnBo> queryColumnListByTableId(String tableId) throws Exception {
        return SystemDatabaseColumnConverter.doList2BoList(systemDatabaseColumnDao.queryColumnListByTableId(tableId));
    }

    /**
     * 分页查询SystemDatabaseColumn
     *
     * @param systemDatabaseColumnBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemDatabaseColumnBo> queryByPage(SystemDatabaseColumnBo systemDatabaseColumnBo, Integer startNum,
        Integer pageSize) throws Exception {
        PageInfo<SystemDatabaseColumnDo> pageInfo = systemDatabaseColumnDao.queryByPage(
            SystemDatabaseColumnConverter.bo2Do(systemDatabaseColumnBo), startNum, pageSize);
        List<SystemDatabaseColumnBo> systemDatabaseColumnBos = SystemDatabaseColumnConverter.doList2BoList(
            pageInfo.getList());
        if (CollectionUtil.isNotEmpty(systemDatabaseColumnBos)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("columnType", "std_mysql_data_type");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(systemDatabaseColumnBos)),
                dictMap);
            systemDatabaseColumnBos = JSON.parseArray(jsonArray.toJSONString(), SystemDatabaseColumnBo.class);
        }
        return new PageInfo<>(pageInfo, systemDatabaseColumnBos);
    }

    /**
     * 分页查询要导入数据条目的SystemDatabaseColumn
     *
     * @param systemDatabaseColumnBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemDatabaseColumnBo> queryImportByPage(SystemDatabaseColumnBo systemDatabaseColumnBo,
        Integer startNum, Integer pageSize) throws Exception {
        PageInfo<SystemDatabaseColumnDo> pageInfo = systemDatabaseColumnDao.queryImportByPage(
            SystemDatabaseColumnConverter.bo2Do(systemDatabaseColumnBo), startNum, pageSize);
        List<SystemDatabaseColumnBo> systemDatabaseColumnBos = SystemDatabaseColumnConverter.doList2BoList(
            pageInfo.getList());
        if (CollectionUtil.isNotEmpty(systemDatabaseColumnBos)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("columnType", "std_mysql_data_type");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(systemDatabaseColumnBos)),
                dictMap);
            systemDatabaseColumnBos = JSON.parseArray(jsonArray.toJSONString(), SystemDatabaseColumnBo.class);
        }
        return new PageInfo<>(pageInfo, systemDatabaseColumnBos);
    }

    /**
     * 根据主键查询SystemDatabaseColumn信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemDatabaseColumnBo getSystemDatabaseColumnById(String id) throws Exception {
        return SystemDatabaseColumnConverter.do2Bo(systemDatabaseColumnDao.getSystemDatabaseColumnById(id));
    }
}
